package com.soap.desensitize.strategy.rreversible.asymmetric;

import com.soap.desensitize.encry.rreversible.asymmetric.AbstractAsymmetryEncry;
import com.soap.desensitize.exception.strategy.DecodeException;
import com.soap.desensitize.exception.strategy.EncodeException;
import com.soap.desensitize.util.AsymmetricUtil;
import com.sun.org.apache.xerces.internal.impl.dv.util.HexBin;
import java.security.*;


public class DsaStrategy extends AbstractAsymmetryEncry<String> {
    private static final String algorithm = "DSA";//算法类型
    private static int defaultkeysize=512;//默认key大小
    private  static  String defalutVerifyType="SHA1withDSA";
    private static AsymmetricUtil utils;//工具类
    static {
        try {
            utils=new AsymmetricUtil(algorithm,defaultkeysize);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }

    private static DsaStrategy instance = null;
    private DsaStrategy(){}

    /**
     * @desc 获得实例
     * @return
     */
    public static synchronized DsaStrategy getInstance(){
        if(instance == null){
            instance = new DsaStrategy();
        }
        return instance;
    }

    @Override
    public String encode(String data) throws EncodeException {
        byte[] bux = creatSignature(data);
        return HexBin.encode(bux);
    }

    /**
     * 将明文转换成数字签名
     * @param data
     * @return
     * @throws SignatureException
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeyException
     */
    public byte[] creatSignature(String data) throws EncodeException {
        try {
            PrivateKey privateKey = utils.getPrivateKey();
            Signature signature = Signature.getInstance(defalutVerifyType);
            signature.initSign(privateKey);
            signature.update(data.getBytes());
            return signature.sign();
        } catch (NoSuchAlgorithmException e) {
            throw new EncodeException("未找到该算法 "+e.getMessage());
        } catch (InvalidKeyException e) {
            throw new EncodeException("无效关联异常 "+e.getMessage());
        } catch (SignatureException e) {
            throw new EncodeException("内核异常签名异常 "+e.getMessage());
        }

    }

    @Override
    public String decode(String data) throws DecodeException {
        throw new DecodeException("不可解密");
    }

    /**
     * 数据和签名进行比对
     * @param newstr
     * @param sgin
     * @return
     */
    public Boolean verifySignature(String newstr,String sgin){

        try {
            return bidui(newstr, sgin);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (SignatureException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }
        return  null;
    }

    private boolean bidui(String newstr, String sgin) throws NoSuchAlgorithmException, SignatureException, InvalidKeyException {
        PublicKey publicKey = utils.getPublicKey();
        Signature signature = Signature.getInstance("SHA1withDSA");
        signature.initVerify(publicKey);
        signature.update(newstr.getBytes());

        return signature.verify(HexBin.decode(sgin));
    }
}
